﻿using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web; 
using System.Web.Mvc;
using System.Reflection;
using System.ComponentModel.DataAnnotations;
using System;

namespace SRF.UI.Html
{
    public static class TextExtensions
    {
        #region Text

        public static MvcHtmlString Text(this HtmlHelper htmlHelper, string format, params object[] values)
        {
            string text = string.Format(format, values);

            return new MvcHtmlString(text);
        }

        [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
        public static MvcHtmlString TextFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TProperty>> expression,
            string format,
            bool submit = false)
        {
            return TextFor(htmlHelper, expression, (m => string.Format(format, m)), submit);
        }

        [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
        public static MvcHtmlString TextFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TProperty>> expression,
            Func<TProperty, string> format = null,
            bool submit = false)
        {
            string text = "", html;
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            if (metadata.Model == null)
            {
                text = "";
            }
            else
            {
                if (format == null)
                {
                    text = metadata.Model.ToString();
                }
                else
                {
                    text = format.Invoke((TProperty)metadata.Model);
                }
            }
            html = text;
            if (submit)
            {
                TagBuilder hiddenTag = new TagBuilder("input");
                hiddenTag.MergeAttribute("id", metadata.PropertyName);
                hiddenTag.MergeAttribute("name", metadata.PropertyName);
                hiddenTag.MergeAttribute("type", "hidden");
                hiddenTag.MergeAttribute("value", text);
                html += hiddenTag.ToString();
            }

            return new MvcHtmlString(html);
        }


        public static MvcHtmlString BooleanText(this HtmlHelper htmlHelper, object value, string[] texts)
        {
            string text = "";
            if (texts.Length > 0)
            {
                if (value == null || value.Equals(false) || value.Equals(0))
                {
                    if (texts.Length > 1)
                        text = texts[1];
                }
                else
                {
                    text = texts[0];
                }
            }

            return new MvcHtmlString(text);
        }

        #endregion
    }
}